package com.zhzhinn.address;

import android.content.Context;


import com.zhzhinn.uc.addresspick.JsonFileReader;
import com.zhzhinn.uc.addresspick.ProvinceBean;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/3/17.
 */
public class AddressManger {

    public static AddressManger instance;

    public static AddressManger getInstance() {
        if (instance == null) {
            instance = new AddressManger();
        }
        return instance;
    }

    //  省份
    ArrayList<ProvinceBean> provinceBeanList = new ArrayList<>();
    //  城市
    ArrayList<String> cities;
    ArrayList<List<String>> cityList = new ArrayList<>();
    //  区/县
    ArrayList<String> district;
    ArrayList<List<String>> districts;
    ArrayList<List<List<String>>> districtList = new ArrayList<>();


    public ArrayList<List<List<String>>> getDistrictList() {
        return districtList;
    }

    public ArrayList<List<String>> getCityList() {
        return cityList;
    }

    public ArrayList<ProvinceBean> getProvinceBeanList() {
        return provinceBeanList;
    }

    //  解析json填充集合
    public void parseJson(Context context) {
        try {
            //  获取json数据
            String province_data_json = JsonFileReader.getJson(context, "province_data.json");
            //  解析json数据
            //  获取json中的数组
            JSONArray jsonArray = new JSONArray(province_data_json);
            //  遍历数据组
            for (int i = 0; i < jsonArray.length(); i++) {
                //  获取省份的对象
                JSONObject provinceObject = jsonArray.optJSONObject(i);
                //  获取省份名称放入集合
                String provinceName = provinceObject.getString("name");
                provinceBeanList.add(new ProvinceBean(i ,provinceName,"描述部分","其他数据"));
                //  获取城市数组
                JSONArray cityArray = provinceObject.optJSONArray("city");
                cities = new ArrayList<>();//   声明存放城市的集合
                districts = new ArrayList<>();//声明存放区县集合的集合
                //  遍历城市数组
                for (int j = 0; j < cityArray.length(); j++) {
                    //  获取城市对象
                    JSONObject cityObject = cityArray.optJSONObject(j);
                    //  将城市放入集合
                    String cityName = cityObject.optString("name");
                    cities.add(cityName);
                    district = new ArrayList<>();// 声明存放区县的集合
                    //  获取区县的数组
                    JSONArray areaArray = cityObject.optJSONArray("area");
                    //  遍历区县数组，获取到区县名称并放入集合
                    for (int k = 0; k < areaArray.length(); k++) {
                        String areaName = areaArray.getString(k);
                        district.add(areaName);
                    }
                    //  将区县的集合放入集合
                    districts.add(district);
                }
                //  将存放区县集合的集合放入集合
                districtList.add(districts);
                //  将存放城市的集合放入集合
                cityList.add(cities);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}
